from rest_framework import serializers
from luckin import models


class UserSerializers(serializers.ModelSerializer):
    class Meta:
        model = models.User
        fields = "__all__"
        depth = 3


class ShippingAddressSerializers(serializers.ModelSerializer):
    class Meta:
        model = models.ShippingAddress
        fields = "__all__"
        depth = 3


class GoodsOrderSerializers(serializers.ModelSerializer):
    class Meta:
        model = models.GoodsOrder
        fields = "__all__"
        depth = 3


class GoodsSerializers(serializers.ModelSerializer):
    class Meta:
        model = models.Goods
        fields = "__all__"
        depth = 3

    specification = serializers.SerializerMethodField()
    temperature = serializers.SerializerMethodField()
    sugar = serializers.SerializerMethodField()
    milk = serializers.SerializerMethodField()
    cream = serializers.SerializerMethodField()

    def get_milk(self, obj):
        data_list = []
        for i in obj.milk.all():
            data_list.append({"id": i.name, "nickname": i.get_name_display(), "is_default": i.is_default})

        return data_list

    def get_cream(self, obj):
        data_list = []
        for i in obj.cream.all():
            data_list.append(
                {"id": i.name, "nickname": i.get_name_display(), "price": i.price, "is_default": i.is_default})

        return data_list

    def get_sugar(self, obj):
        data_list = []
        for i in obj.sugar.all():
            data_list.append(
                {"id": i.name, "nickname": i.get_name_display(), "price": i.price, "is_default": i.is_default})

        return data_list

    def get_temperature(self, obj):
        data_list = []
        for i in obj.temperature.all():
            data_list.append({"id": i.name, "nickname": i.get_name_display(), "is_default": i.is_default})

        return data_list

    def get_specification(self, obj):
        data_list = []
        for i in obj.specification.all():
            data_list.append(
                {"id": i.name, "nickname": i.get_name_display(), "price": i.price, "salt_price": i.salt_price,
                 "is_default": i.is_default})
        return data_list
